package cn.itsource.product.service.impl;

import cn.itsource.basic.util.BaiduAuditUtils;
import cn.itsource.product.domain.Product;
import cn.itsource.product.domain.ProductDetail;
import cn.itsource.product.domain.Product;
import cn.itsource.product.domain.ProductOnlineAuditLog;
import cn.itsource.product.mapper.ProductMapper;
import cn.itsource.product.mapper.ProductOnlineAuditLogMapper;
import cn.itsource.product.service.IProductDetailService;
import cn.itsource.product.service.IProductService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-11-03
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private IProductDetailService productDetailService;

    @Override
    public void onsale(List<Long> ids) {
        Map<Long, Product> productMaps = productMapper.findAll().stream().filter(product -> product.getState() == 0).collect(Collectors.toMap(Product::getId, product -> product));
        Date date = new Date();
        for (Long id : ids) {
            Product product = productMaps.get(id);
            StringBuilder sb = new StringBuilder();
            // 不等于null,说明数据本身就是下架的
            if (product != null){
                if (!BaiduAuditUtils.TextCensor(product.getName())){
                    sb.append("服务上架百度AI审核名称失败，服务名称为：" + product.getName() + ";");
                }
                if (product.getResources().split(",").length > 1){
                    if (!BaiduAuditUtils.ImgCensor(product.getResources().split(",")[0])){
                        sb.append("服务上架百度AI审核图片失败，服务图片地址为：" + product.getResources() + ";");
                    }
                }else {
                    if (!BaiduAuditUtils.ImgCensor(product.getResources())) {
                        sb.append("服务上架百度AI审核图片失败，服务图片地址为：" + product.getResources() + ";");
                    }
                }
                if (sb.length() > 0){
                    ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                    productOnlineAuditLog.setAuditTime(date);
                    productOnlineAuditLog.setProductId(id);
                    productOnlineAuditLog.setNote(sb.toString());
                    productOnlineAuditLog.setState(0);
                    productOnlineAuditLogMapper.add(productOnlineAuditLog);
                    continue;
                }
                product.setState(1L);
                product.setOnsaletime(date);
                // 服务上架之后，设置原来的夏季时间
                product.setOffsaletime(null);
                productMapper.update(product);
            }
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        productMapper.batchOffsale(ids);
    }

    @Override
    public Product getProductDetail(Long id) {
        return productMapper.getProductDetail(id);
    }
    @Override
    @Transactional
    public void add(Product product) {
        super.add(product);
        ProductDetail productDetail = null;
        if (product.getProductDetail() == null){
            productDetail = new ProductDetail();
        }else {
            productDetail = product.getProductDetail();
        }

        productDetail.setProductId(product.getId());
        productDetailService.add(productDetail);
    }
}
